export default {
  addLink(url, doc) {
    if (!doc) doc = document;
    const link = doc.createElement("link");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = url;
    link.id = "link_" + encodeURIComponent(url);
    doc.getElementsByTagName("head")[0].appendChild(link);
    return link.id;
  },
  removeLink(id, doc) {
    if (!doc) doc = document;
    const el = doc.getElementById(id);
    if (el && el.nodeType === 1) {
      el.parentNode.removeChild(el);
    }
  },
  addStyle(cssText, doc) {
    if (!doc) doc = document;
    const head = doc.head || doc.getElementsByTagName("head")[0];
    const style = doc.createElement("style");
    style.type = "text/css";
    style.id = "async_style_" + Date.now();
    if (style.styleSheet) {
      style.styleSheet.cssText = cssText;
    } else {
      const textNode = doc.createTextNode(cssText);
      style.appendChild(textNode);
    }
    head.appendChild(style);
    return style.id;
  },
  removeElem(id, doc) {
    if (!doc) doc = document;
    const el = doc.getElementById(id);
    if (el && el.nodeType === 1) {
      el.parentNode.removeChild(el);
    }
  },
};
